package com.app114.app.android.rk;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ShareCompat;
import br.com.dina.ui.widget.UITableView;
import br.com.dina.ui.widget.UITableView.ClickListener;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.app114.app.android.rk.conf.Conf;
import com.app114.app.android.rk.utils.DataFactory;
import com.app114.app.android.rk.utils.DeviceUtil;
import com.umeng.api.sns.UMSNSException;
import com.umeng.api.sns.UMSnsService;
import com.umeng.api.sns.UMSnsService.OauthCallbackListener;
import com.umeng.api.sns.UMSnsService.SHARE_TO;

public class AccountActivity extends BaseFragmentActivity implements ClickListener, OauthCallbackListener {
	private UITableView mTableView;
	private String title;

	@Override
	protected void activitySetup(Bundle savedInstanceState) {
		Intent intent = getIntent();
		if (intent == null || (title = intent.getAction()) == null) {
			return;
		}

		setContentView(R.layout.activity_catagory_list);
		getSupportActionBar().setTitle(title);

		mTableView = (UITableView) findViewById(R.id.tableView);
		createList();
		mTableView.commit();
	}

	private void createList() {
		mTableView.setClickListener(this);
		mTableView.clear();
		if (UMSnsService.isAuthorized(mContext, UMSnsService.SHARE_TO.SINA)) {
			mTableView.addBasicItem(DataFactory.SHARE_ITEM[0], "已经绑定,点击可解除绑定", R.color.green);
		} else {
			mTableView.addBasicItem(DataFactory.SHARE_ITEM[0]);
		}
		if (UMSnsService.isAuthorized(mContext, UMSnsService.SHARE_TO.TENC)) {
			mTableView.addBasicItem(DataFactory.SHARE_ITEM[1], "已经绑定,点击可解除绑定", R.color.green);
		} else {
			mTableView.addBasicItem(DataFactory.SHARE_ITEM[1]);
		}
		if (UMSnsService.isAuthorized(mContext, UMSnsService.SHARE_TO.RENR)) {
			mTableView.addBasicItem(DataFactory.SHARE_ITEM[2], "已经绑定,点击可解除绑定", R.color.green);
		} else {
			mTableView.addBasicItem(DataFactory.SHARE_ITEM[2]);
		}
	}

	@Override
	protected boolean _onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_share, menu);
		return true;
	}

	@Override
	protected boolean _onOptionsItemSelected(MenuItem item) {
		ShareCompat.IntentBuilder.from(this).setType("text/plain").setText(DataFactory.SHARE_INFO + "\r\n" + Conf.URL_APP).startChooser();
		return true;
	}

	@Override
	protected void activityDestory() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(int index) {
		switch (index) {
		case 0:
			if (UMSnsService.isAuthorized(this, UMSnsService.SHARE_TO.SINA)) {
				UMSnsService.writeOffAccount(this, UMSnsService.SHARE_TO.SINA);
				DeviceUtil.toast(this, "已经解除绑定新浪微博");
			} else {
				UMSnsService.oauthSina(this, this);
			}
			break;
		case 1:
			if (UMSnsService.isAuthorized(this, UMSnsService.SHARE_TO.TENC)) {
				UMSnsService.writeOffAccount(this, UMSnsService.SHARE_TO.TENC);
				DeviceUtil.toast(this, "已经解除绑定腾讯微博");
			} else {
				UMSnsService.oauthTenc(this, this);
			}
			break;
		case 2:
			if (UMSnsService.isAuthorized(this, UMSnsService.SHARE_TO.RENR)) {
				UMSnsService.writeOffAccount(this, UMSnsService.SHARE_TO.RENR);
				DeviceUtil.toast(this, "已经解除绑定人人网");
			} else {
				UMSnsService.oauthRenr(this, this);
			}
			break;

		default:
			break;
		}

		createList();
		mTableView.commit();
	}

	@Override
	public void onComplete(Bundle arg0, SHARE_TO arg1) {
		DeviceUtil.toast(this, arg1 + " 绑定成功");
		createList();
		mTableView.commit();
	}

	@Override
	public void onError(UMSNSException arg0, SHARE_TO arg1) {
		DeviceUtil.toast(this, arg1 + " 绑定失败");
	}
}
